package com.gxa.controller;

import com.gxa.pojo.Doctor;
import com.gxa.service.DoctorService;
import com.gxa.util.JsonResult;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.print.Doc;
import java.util.List;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2020/9/22 16:45
 * @Version 1.0
 */

@RestController
@RequestMapping("/doctor")  //处理器获得相应的请求，找到对应控制器的方法
@Api(value = "执行管理员新增医生的接口",tags = "doctor-contorller") // Swagger生成接口规范文档  访问方式：默认访问地址加上   swagger-ui.html
@CrossOrigin //跨域注解
public class DoctorController {

    @Resource
    DoctorService doctorService;

    //测试接口
    @PostMapping("hello")
    @ResponseBody
    public void hello(){
        System.out.println("hello  Test");
    }

    //根据当前对象的ID删除
    @PostMapping(value = "delete")
    @ResponseBody
    public Map deleteById(@RequestBody Map<String,String> map){

        System.out.println("进行删除方法");
        System.out.println("===================");
        Integer id =Integer.valueOf(map.get("id"));
        System.out.println("获取到的ID："+map.get("id"));
        Integer integer = doctorService.deleteById(id);
        JsonResult jsonResult = new JsonResult();
        if(integer > 0){
            jsonResult.setCode("200");
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差");
        }

        return jsonResult.getValues();
    }

    //用户端--->我的医生模块--->查看医生详细信息  &  管理员模块--->医生管理--->查看医生
    @PostMapping(value = "/findDoc")
    @ResponseBody
    public Map findDoc(@RequestBody Map<String,Integer> map){

        System.out.println("接收到的对象："+map);
        System.out.println("=====================");
        System.out.println("接收到的ID："+map.get("id"));

        Map byId = doctorService.findById(map.get("id"));
        JsonResult jsonResult = new JsonResult();
        if(byId == null | byId.equals(" ")){
            jsonResult.setCode("404");
            jsonResult.setMsg("该医生不存在");
        }else {
            jsonResult.setCode("200");
            jsonResult.setMsg("成功");
            jsonResult.setData(byId);
        }
        return jsonResult.getValues();
    }

    //管理员模块--->医生管理--->新增医生 &  医生端——> 添加医生
    @PostMapping(value = "/add")
    @ResponseBody
    public Integer insert(@RequestBody Doctor doctor){
        System.out.println("接受到的对象："+doctor);

        System.out.println("==========================");
        Integer insert = doctorService.insert(doctor);
        JsonResult jsonResult = new JsonResult();
        if(insert > 0){
            jsonResult.setCode("200");
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差");
        }
        return insert;
    }

    //管理员模块--->医生管理：根据姓名、所属医院查询
    @PostMapping(value = "/find")
    @ResponseBody
    public Map findByNH(@RequestBody Map<String,String> map){
        System.out.println(map);
        String name = map.get("name");
        Integer hospitalId = Integer.valueOf(map.get("hospitalId"));
        Map byNameHospital = doctorService.findByNameHospital(name, hospitalId);

        JsonResult jsonResult = new JsonResult();
        if(byNameHospital == null | byNameHospital.equals(" ")){
            jsonResult.setCode("407");
            jsonResult.setMsg("该医生不存在");
        }else {
            jsonResult.setCode("200");
            jsonResult.setMsg("成功");
            jsonResult.setData(byNameHospital);
        }

        return jsonResult.getValues();
    }


    //管理员模块--->医生管理 & 用户端--->我的医生模块--->所有医生的信息显示、医生详情页  的分页查询
    @PostMapping(value = "/limit")
    @ResponseBody
    public  Map findByLimit(){
        List<Map<String, Object>> byLimit = doctorService.findByLimit();
        JsonResult jsonResult = new JsonResult();
        if(byLimit == null | byLimit.equals(" ")){
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差");
        }else {
            jsonResult.setCode("200");
            jsonResult.setMsg("成功");
            jsonResult.setData(byLimit);
        }
        return  jsonResult.getValues();

    }

    //管理员模块--->医生管理--->编辑医生信息  &  医生端——->  编辑医生
    @PostMapping( value = "/update")
    @ResponseBody
    public Map updateById(@RequestBody Doctor doctor){

        System.out.println("获取到的对象："+doctor);;
        System.out.println("=======================");
        int i = doctorService.updateByid(doctor);
        JsonResult jsonResult = new JsonResult();
        if(i > 0){
            jsonResult.setCode("200");
            jsonResult.setMsg("成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差");
        }
        return jsonResult.getValues();
    }
}
